package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    //分页查询所有和条件查询
    @Override
    public Page<Setmeal> findByName(Integer pageNum, Integer pageSize, String name) {
    //构建条件
        LambdaQueryWrapper<Setmeal> lw=new LambdaQueryWrapper<>();
        lw.like(StrUtil.isNotEmpty(name),Setmeal::getName,name);//通过关键字查询
        Page<Setmeal> page=new Page<>(pageNum,pageSize);//添加分页数据
        //调用mapper查询
        page = setmealMapper.selectPage(page, lw);
        List<Setmeal> list = page.getRecords();
        for (Setmeal setmeal : list) {
            //构建条件
            //LambdaQueryWrapper<Category> lq=new LambdaQueryWrapper<>();
            //lq.eq(Category::getId,setmeal.getCategoryId());
            //mapper查询
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            String name1 = category.getName();
            //将列表封装
            setmeal.setCategoryName(name1);
            //构建条件
            LambdaQueryWrapper<SetmealDish> lqw=new LambdaQueryWrapper<>();
            lqw.eq(SetmealDish::getSetmealId,setmeal.getId());
            //mapper查询
            List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lqw);
            //将列表封装
            setmeal.setSetmealDishes(setmealDishes);


        }
        return page ;

    }
    //新增套餐
    @Override
    public void save(Setmeal setmeal) {
        //直接保存菜品基本信息
        setmealMapper.insert(setmeal);
        //获取并遍历菜品
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //关联套餐id
            setmealDish.setSetmealId(setmeal.getId());
            //存入数据库
            setmealDishMapper.insert(setmealDish);
        }
    }
    //删除套餐
    @Override
    public void deleteBatchIds(List<Long> ids) {
        //// 1.先判断菜品是否处于停售状态
        //select count(*) from setmeal where status = 1 and  id in (xx,xx,xxx)
        //构建条件
        LambdaQueryWrapper<Setmeal> lw=new LambdaQueryWrapper<>();
        lw.eq(Setmeal::getStatus,1);
        lw.in(Setmeal::getId,ids);
        //调用mapper查询
        Integer integer = setmealMapper.selectCount(lw);
        //判断结果是否大于0
        if (integer>0){
            throw new CustomException("删除中包含未停售的菜品");
        }
        //通过id删除 delete from setmeal where id in (xx,xx,xxx)
        setmealMapper.deleteBatchIds(ids);
        //删除套餐中的菜品类中的菜品
        LambdaQueryWrapper<SetmealDish> qw=new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(qw);
    }


    //修改回显套餐
    @Override
    public Setmeal findById(Long id) {
        //根据id查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //根据id查询套餐菜品列表
        // 2-1 构建条件
        LambdaQueryWrapper<SetmealDish> lw=new LambdaQueryWrapper<>();
        lw.eq(SetmealDish::getSetmealId,id);
        //执行查询
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lw);
        //将菜品列表设置到套餐中
        setmeal.setSetmealDishes(setmealDishes);
        //返回菜品
        return setmeal;

    }
    //修改套餐
    @Override
    public void update(Setmeal setmeal) {
        //调用service
        setmealMapper.updateById(setmeal);
        //构造条件
        //先删除旧套餐内容
        LambdaQueryWrapper<SetmealDish> lq=new LambdaQueryWrapper<>();
        lq.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(lq);
        //获取遍历添加新套餐内容
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //关联菜品id
            setmealDish.setSetmealId(setmeal.getId());

           //存入数据库
           setmealDishMapper.insert(setmealDish);
        }
    }
    //批量启停售卖
    @Override
    public void sellStatus(Integer type, List<Long> ids) {
        //构建条件
        LambdaQueryWrapper<Setmeal> lq=new LambdaQueryWrapper<>();
        lq.in(Setmeal::getId,ids);
        //创建对象
        Setmeal setmeal=new Setmeal();
        //添加修改内容
        setmeal.setStatus(type);
        //执行修改
        setmealMapper.update(setmeal,lq);
    }
    //客户端展示套餐内容
    @Override
    public List<Setmeal> findList(Long category, Integer status) {
        //构建条件   select * from setmeal where category_id = ? and status = ?
        LambdaQueryWrapper<Setmeal> lq=new LambdaQueryWrapper<>();
        lq.eq(Setmeal::getCategoryId,category);
        lq.eq(Setmeal::getStatus,status);
        //调用maapper
        List<Setmeal> list = setmealMapper.selectList(lq);
        //返回结果
        return list;

    }
    //展示套餐详情
    @Override
    public List<Dish> show(Long id) {
        //构建条件 查询套餐中菜品
        LambdaQueryWrapper<SetmealDish> lq=new LambdaQueryWrapper<>();
        lq.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lq);
        List<Dish> dishList = new ArrayList<>();
    // 遍历 查询菜品中图片
        for (SetmealDish setmealDish : setmealDishes) {
            //构建条件
            LambdaQueryWrapper<Dish> qq=new LambdaQueryWrapper<>();
            qq.eq(Dish::getId,setmealDish.getDishId());
            Dish dish = dishMapper.selectOne(qq);
            // 2-2 添加套餐中数量
            dish.setCopies(setmealDish.getCopies());
            // 2-3 添加返回菜品集合
            dishList.add(dish);

        }
        // 3.返回结果
        return dishList;
    }

}
